'use strict';

const Controller = require('../core/base_controller');

class Menu extends Controller {

	async list() {

		const { app, ctx } = this;

		const res = await ctx.service.menu.list();

		this.success(res);
	}

	
	async create () {
		const { app, ctx } = this;

		const {id, pid, name, href, icon, sort, isshow, del } = ctx.request.body;

		let res = undefined;
		
		if(id == undefined){
			//创建
			res = await ctx.service.menu.create(pid, name, href, icon, sort, isshow);
		}else{
			if(del == undefined){
				//修改
				res = await ctx.service.menu.update(name, href, icon, sort, isshow, id);
			}else{
				//删除
				res = await ctx.service.menu.delete(id);
			}
		}

		this.success(res);
 	}


 	async getUserMenu () {

 		const { app, ctx } = this;

 		console.log(ctx._user);
 		console.log(ctx._role);

 		let roleids = [];

 		ctx._role.forEach(item => {
 			roleids.push(item.id);
 		});

 		const result = await ctx.service.menu.getUserMenu(roleids);

 		this.success(result);
 	}


 	async getMenuByRole() {
 		const { app, ctx } = this;
 		const { roleId } = ctx.request.body;

 		const result = await ctx.service.menu.getMenuByRole(roleId);

 		this.success(result);
 	}
}

module.exports = Menu;
